package com.imooc.girl.controller;

import com.imooc.girl.domain.Girl;
import com.imooc.girl.exception.GirlException;
import com.imooc.girl.repository.GirlRepository;
import com.imooc.girl.result.Result;
import com.imooc.girl.result.ResultHandler;
import com.imooc.girl.service.GirlService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/girl")
public class GirlController {

    private static final Logger log = LoggerFactory.getLogger(GirlController.class);

    @Autowired
    private GirlRepository girlRepository;

    @Autowired
    private GirlService girlService;

    @GetMapping("/getGirlList")
    public Map<String, Object> getGirlList() {
        System.out.println("list111");
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("data", girlRepository.findAll());
        result.put("errorCode", "0");
        result.put("msg", "查询成功");
        return result;
    }

    @PostMapping("/saveGirl")
    public Result<Girl> saveGirl(@Valid Girl girl, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.error("errorMsg:{}, method:saveGirl, args:{}", bindingResult.getFieldError().getDefaultMessage(), girl.toString());
            return ResultHandler.error("-1", bindingResult.getFieldError().getDefaultMessage());
        }
        girl.setAge(girl.getAge());
        girl.setCupSize(girl.getCupSize());
        return ResultHandler.successWithMsg(girl, "新增成功");
    }

    @GetMapping("/{id}/getGirlById")
    public Map<String, Object> getGirlById(@PathVariable("id") Integer id) {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("data", girlRepository.findOne(id));
        result.put("errorCode", "0");
        result.put("msg", "查询成功");
        return result;
    }

    @PostMapping("/{id}/updateGirl")
    public Map<String, Object> updateGirl(@PathVariable("id") Integer id, @RequestParam("cupSize") String cupSize, @RequestParam("age") Integer age) {
        Map<String, Object> result = new HashMap<String, Object>();
        Girl girl = new Girl();
        girl.setId(id);
        girl.setAge(age);
        girl.setCupSize(cupSize);
        result.put("data", girlRepository.save(girl));
        result.put("errorCode", "0");
        result.put("msg", "修改成功");
        return result;
    }

    @PostMapping("/{id}/deleteGirl")
    public Map<String, Object> deleteGirl(@PathVariable("id") Integer id) {
        Map<String, Object> result = new HashMap<String, Object>();
        girlRepository.delete(id);
        result.put("errorCode", "0");
        result.put("msg", "删除成功");
        return result;
    }

    @GetMapping("/{age}/getGirlByAge")
    public Map<String, Object> getGirlByAge(@PathVariable("age") Integer age) {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("data", girlRepository.findByAge(age));
        result.put("errorCode", "0");
        result.put("msg", "查询成功");
        return result;
    }

    @GetMapping("/getAge/{id}")
    public void getAge(@PathVariable("id") Integer id) throws GirlException {
        girlService.getAge(id);
    }

}
